package cl.ucinf.progii.solemne1.data;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Test {

	public static void main(String[] args) {

		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
		try {
			conn = DriverManager.getConnection("jdbc:mysql://localhost/progii?" + 
				"user=root&password=ucinf");
		} catch (SQLException ex) {
			System.out.println("SQLException: " + ex.getMessage());
			System.out.println("SQLState: " + ex.getSQLState());
			System.out.println("VendorError: " + ex.getErrorCode());
		}
		
		
		try {
			
			
			PreparedStatement pstmt = conn.prepareStatement("INSERT INTO usuario values (null, ?, ?, ?, ?)");
			pstmt.setString(1, "0123456789012345678901234567890123456789");
			pstmt.setString(2, "2");
			pstmt.setString(3, "3");
			pstmt.setString(4, "4");
			
			pstmt.execute();
			pstmt.close();
			
			
			stmt = conn.createStatement();
			rs = stmt.executeQuery("select * from usuario;");
			while (rs.next()){
				System.out.println(rs.getString("nombre") + rs.getString("apellido") );
			}
		}catch (SQLException ex){
			System.out.println("SQLException: " + ex.getMessage());
			System.out.println("SQLState: " + ex.getSQLState());
			System.out.println("VendorError: " + ex.getErrorCode());
		}
		
		
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException sqlEx) { } // ignore
				rs = null;
			}
		if (stmt != null) {
			try {
				stmt.close();
			} catch (SQLException sqlEx) { } // ignore
				stmt = null;
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException sqlEx) { } // ignore
				conn = null;
		}
		
		
	}

}
